package cn.jinjimi.app.custom.ui.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.CustomCount;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.custom.ui.activity.AddFollowRecordActivity;
import cn.jinjimi.app.custom.ui.activity.CustomClueActivity;
import cn.jinjimi.app.custom.ui.activity.CustomEditOrAddActivity;
import cn.jinjimi.app.custom.ui.activity.CustomInputActivity;
import cn.jinjimi.app.custom.ui.activity.CustomLastedFollowListActivity;
import cn.jinjimi.app.custom.ui.activity.CustomerTypeListActivity;
import cn.jinjimi.app.managers.NoticeManager;
import cn.jinjimi.app.store.entity.NoticeEntity;
import cn.jinjimi.app.main.widget.RedNumTextView;

/**
 * Created by Roy
 * Date: 16/6/24
 */
public class CustomAllHeadView extends LinearLayout implements View.OnClickListener {

    private TextView tvClue;
    private TextView tvFollow;
    private TextView tvUnsold;
    private TextView tvSold;

    private RedNumTextView tvUnread;

    public CustomAllHeadView(Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.fragment_all_customer_header_view, this);
        initView();
        initListener();
    }

    private void initView() {
        tvClue = (TextView) findViewById(R.id.tv_clue_num);
        tvFollow = (TextView) findViewById(R.id.tv_follow_num);
        tvUnsold = (TextView) findViewById(R.id.tv_unsold_num);
        tvSold = (TextView) findViewById(R.id.tv_sold_num);

        tvUnread = (RedNumTextView) findViewById(R.id.tv_unread);
    }

    private void initListener() {
        findViewById(R.id.ll_clue).setOnClickListener(this);
        findViewById(R.id.ll_follow).setOnClickListener(this);
        findViewById(R.id.ll_unsold).setOnClickListener(this);
        findViewById(R.id.ll_solded).setOnClickListener(this);

        findViewById(R.id.ll_import).setOnClickListener(this);
        findViewById(R.id.ll_add_customer).setOnClickListener(this);
        findViewById(R.id.ll_add_follow).setOnClickListener(this);
    }

    /* 显示准客户和已签单客户的数据 */
    public void setCustomCount() {
        CustomCount customCount = CustomsManager.getInstance().getCustomCount();
        tvClue.setText(String.format(getContext().getString(R.string.num), customCount.clueCount));
        tvFollow.setText(String.format(getContext().getString(R.string.num), customCount.recentCount));
        tvUnsold.setText(String.format(getContext().getString(R.string.num), customCount.potentialCount));
        tvSold.setText(String.format(getContext().getString(R.string.num), customCount.contractCount));
    }

    /* 推广获客的数据 */
    public void setDrpCount(int count) {
        tvUnread.setUnreadNum(count);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_clue:// 展业获客
                enterZhanye();
                break;
            case R.id.ll_follow:// 最近跟进
                enterFollows();
                break;
            case R.id.ll_unsold:// 准客户
                enterUnsoldCustomer();
                break;
            case R.id.ll_solded:// 已签单客户
                enterSoldedCustomer();
                break;
            case R.id.ll_add_customer: // 从通讯录导入
                addCustomerWithHand();
                break;
            case R.id.ll_import: // 手工添加客户
                addCustomerFormContact();
                break;
            case R.id.ll_add_follow: // 点击添加跟进记录
                addFollowRecord();
                break;
        }
    }

    @EventTrace(EventID.APP_CUSTOMER_INDEX_CUSTOMER_PAID_CLICK)
    private void enterSoldedCustomer() {
        CustomerTypeListActivity.lanch(getContext(), Custom.TYPE_SOLDED_CUSTOMER);
    }

    @EventTrace(EventID.APP_CUSTOMER_INDEX_CUSTOMER_READY_CLICK)
    private void enterUnsoldCustomer() {
        CustomerTypeListActivity.lanch(getContext(), Custom.TYPE_UNSOLD_CUSTOMER);
    }

    @EventTrace(EventID.APP_CUSTOMER_INDEX_RECENT)
    private void enterFollows() {
        CustomLastedFollowListActivity.launch(getContext());
    }

    @EventTrace(EventID.APP_CUSTOMER_INDEX_ZHANYE)
    private void enterZhanye() {
        CustomClueActivity.launch(getContext());
        NoticeManager.clickNoticeByCode(NoticeEntity.TypeNewClue);
    }

    /* 从通讯录导入 */
    @EventTrace(EventID.APP_CUSTOM_ADD_ADDRESS)
    private void addCustomerFormContact() {
        CustomInputActivity.launch(getContext());
    }

    /* 手工添加客户 */
    @EventTrace(EventID.APP_CUSTOM_ADD_MANUAL)
    private void addCustomerWithHand() {
        CustomEditOrAddActivity.launch(getContext());
    }

    /* 点击添加跟进记录 */
    @EventTrace(EventID.APP_CUSTOMER_INDEX_MENU_ADDFOLLOW)
    private void addFollowRecord() {
        AddFollowRecordActivity.launch(getContext(), null, true, true);
    }
}